BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

State Design Pattern

The state pattern is a design pattern that allows an object to completely change its behaviour depending upon its current internal state. By substituting classes within a defined context, the state object appears to change its type at run-time.

What is the State Pattern?

The state pattern is a Gang of Four design pattern. This is a behavioural pattern as it defines a manner for controlling communication between classes or entities. The state pattern is used to alter the behaviour of an object as its internal state changes. The pattern allows the class for an object to change at run-time without changing the interface used to access the object or losing the current state. The class change is hidden to the outside world with the use of a wrapper object, or context.

The state pattern is useful when creating object-oriented state machines, where the functionality of an object changes fundamentally according to its state. By using multiple concrete classes, each inheriting from the same base class, large differences in functionality are possible without resorting to numerous "if" or "switch" statements.

An example of the state design pattern could be used for the control panel of a simple media player. When the device is playing MP3 music, the "play" button could pause and restart the music. When the user is listening to the radio, the same button may search for the next radio station. To switch between the states of MP3 playback, radio tuning and standby mode, the user could press the "audio source" button.

Implementing the State Pattern

State Design Pattern UML

The UML class diagram above describes an implementation of the state design pattern. The items in the diagram are described below:

  • Context. The Context class is used by clients of the state design pattern. Clients do not access the state objects directly. The Context class holds a concrete state object that provides the behaviour according to its current state.
  • StateBase. This abstract class is the base class for all concrete state classes. StateBase defines the interface that will be used by the Context object to access the changeable functionality. No state, in terms of fields or properties, is defined in the StateBase class or its subclasses.
  • ConcreteState A/B. The concrete state classes provide the real functionality that will be used by the Context object. Each state class provides behaviour that is applicable to a single state of the Context object. They may also include instructions that cause the Context to change its state.

The following shows the basic code of the state design pattern implemented using C#. Note that the starting state is passed to the Context object via a constructor. As calls are made to the Request method they are routed to the current State object's Handle method. This outputs a message and switches the state of the Context.

public class Context
{
    private StateBase _state;

    public Context(StateBase state)
    {
        _state = state;
    }

    public void Request()
    {
        _state.Handle(this);
    }

    public StateBase State
    {
        get { return _state; }
        set { _state = value; }
    }
}


public abstract class StateBase
{
    public abstract void Handle(Context context);
}


public class ConcreteStateA : StateBase
{
    public override void Handle(Context context)
    {
        Console.WriteLine("Handle called from ConcreteStateA");
        context.State = new ConcreteStateB();
    }
}


public class ConcreteStateB : StateBase
{
    public override void Handle(Context context)
    {
        Console.WriteLine("Handle called from ConcreteStateB");
        context.State = new ConcreteStateA();
    }
}
21 July 2009